package com.aries.ui.widget.demo.module.alert;

import com.aries.ui.widget.BasisDialog;
import com.aries.ui.helper.navigation.KeyboardHelper;
import com.aries.ui.util.ResourceUtil;
import com.aries.ui.view.radius.RadiusEditText;
import com.aries.ui.view.radius.RadiusTextView;
import com.aries.ui.widget.alert.UIAlertDialog;
import com.aries.ui.widget.demo.ResourceTable;
import com.aries.ui.widget.demo.base.BaseAbilitySlice;
import com.aries.ui.widget.demo.util.SizeUtil;
import com.aries.ui.widget.i.DialogInterface;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbPalette;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.IDialog;
import ohos.agp.window.dialog.ToastDialog;
import ohos.multimodalinput.event.KeyEvent;

/**
 * @Author: AriesHoo on 2019/4/11 12:21
 * @E-Mail: AriesHoo@126.com
 * @Function: UIAlertDialog示例
 * @Description:
 */

public class AlertAbilitySlice extends BaseAbilitySlice implements Component.ClickedListener {

    IDialog.ClickedListener onAlertClick = new IDialog.ClickedListener() {
        @Override
        public void onClick(IDialog dialog, int which) {
            String msg = "";
            switch (which) {
                case DialogInterface.BUTTON_NEGATIVE:
                    msg = "否定";
                    break;
                case DialogInterface.BUTTON_POSITIVE:
                    msg = "肯定";
                    break;
                case DialogInterface.BUTTON_NEUTRAL:
                    msg = "中性";
                    break;
            }
            if (dialog instanceof UIAlertDialog) {
                msg = ((UIAlertDialog) dialog).getButton(which).getText().toString();
            }
            showToast(msg);
        }
    };
    private Slider sBarNum;
    private Text tvNum;
    private RadiusTextView rtvShowAlert;
    private RadiusTextView rtvShowQqAlert;
    private RadiusTextView rtvShowAllAlert;
    private RadiusTextView rtvEditAlert;
    private RadiusTextView rtvFragmentAlert;
    private DependentLayout sBtnTitleAlert;
    private DependentLayout sBtnTitleColorAlert;
    private DependentLayout sBtnMsgColorAlert;
    private DependentLayout sBtnButtonColorAlert;
    private DependentLayout sBtnBackAlert;
    private Text sTvShowTitle;
    private Text sTvShowTitleColor;
    private Text sTvShowMsgColor;
    private Text sTvShowButtonColor;
    private Text sTvShowBack;
    private Switch sTmShowTitle;
    private Switch sTmShowTitleColor;
    private Switch sTmShowMsgColor;
    private Switch sTmShowButtonColor;
    private Switch sTmShowBack;
    private boolean isShowTitle = true;
    private boolean isDefaultTitleColor = false;
    private boolean isDefaultMsgColor = false;
    private boolean isDefaultButtonColor = false;
    private boolean isBackDim = true;
    private int num = 2;
    private String mFormatName = "%1s<font color=\"red\">%2s</font>%3s";

    @Override
    protected void setTitleBar() {
        titleBar.setTitleMainText("UIAlertDialog示例").setStatusBackgroundColor(RgbPalette.WHITE.asArgbInt());
    }

    @Override
    protected int getLayout() {
        return ResourceTable.Layout_ability_alert;
    }

    @Override
    protected void initView(Intent intent) {
        sBarNum = (Slider) findComponentById(ResourceTable.Id_sBar_num);
        tvNum = (Text) findComponentById(ResourceTable.Id_tv_statusNum);

        rtvShowAlert = (RadiusTextView) findComponentById(ResourceTable.Id_rtv_showAlert);
        rtvShowQqAlert = (RadiusTextView) findComponentById(ResourceTable.Id_rtv_showQqAlert);
        rtvShowAllAlert = (RadiusTextView) findComponentById(ResourceTable.Id_rtv_showAllAlert);
        rtvEditAlert = (RadiusTextView) findComponentById(ResourceTable.Id_rtv_editAlert);
        rtvFragmentAlert = (RadiusTextView) findComponentById(ResourceTable.Id_rtv_fragmentAlert);

        sBtnTitleAlert = (DependentLayout) findComponentById(ResourceTable.Id_sBtn_titleAlert);
        sBtnTitleColorAlert = (DependentLayout) findComponentById(ResourceTable.Id_sBtn_titleColorAlert);
        sBtnMsgColorAlert = (DependentLayout) findComponentById(ResourceTable.Id_sBtn_msgColorAlert);
        sBtnButtonColorAlert = (DependentLayout) findComponentById(ResourceTable.Id_sBtn_buttonColorAlert);
        sBtnBackAlert = (DependentLayout) findComponentById(ResourceTable.Id_sBtn_backAlert);

        sTvShowTitle = (Text) findComponentById(ResourceTable.Id_sTv_show_title);
        sTvShowTitleColor = (Text) findComponentById(ResourceTable.Id_sTv_show_titleColor);
        sTvShowMsgColor = (Text) findComponentById(ResourceTable.Id_sTv_show_msgColor);
        sTvShowButtonColor = (Text) findComponentById(ResourceTable.Id_sTv_show_buttonColor);
        sTvShowBack = (Text) findComponentById(ResourceTable.Id_sTv_show_back);

        sTmShowTitle = (Switch) findComponentById(ResourceTable.Id_sTm_show_title);
        sTmShowTitleColor = (Switch) findComponentById(ResourceTable.Id_sTm_show_titleColor);
        sTmShowMsgColor = (Switch) findComponentById(ResourceTable.Id_sTm_show_msgColor);
        sTmShowButtonColor = (Switch) findComponentById(ResourceTable.Id_sTm_show_buttonColor);
        sTmShowBack = (Switch) findComponentById(ResourceTable.Id_sTm_show_back);

        sBtnTitleAlert.setClickedListener(this);
        sBtnTitleColorAlert.setClickedListener(this);
        sBtnMsgColorAlert.setClickedListener(this);
        sBtnButtonColorAlert.setClickedListener(this);
        sBtnBackAlert.setClickedListener(this);

        rtvShowAlert.setClickedListener(this);
        rtvShowQqAlert.setClickedListener(this);
        rtvShowAllAlert.setClickedListener(this);
        rtvEditAlert.setClickedListener(this);
        rtvFragmentAlert.setClickedListener(this);

        sBarNum.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean b) {
                num = progress + 1;
                tvNum.setText(num + "");
            }

            @Override
            public void onTouchStart(Slider slider) {

            }

            @Override
            public void onTouchEnd(Slider slider) {

            }
        });

        sTmShowTitle.setCheckedStateChangedListener((absButton, isChecked) -> {
            isShowTitle = isChecked;
            sTvShowTitle.setText(isChecked ? "显示Title" : "隐藏Title");
            sBtnTitleColorAlert.setVisibility(isChecked ? Component.VISIBLE : Component.HIDE);
        });

        sTmShowTitleColor.setCheckedStateChangedListener((absButton, isChecked) -> {
            isDefaultTitleColor = isChecked;
            sTvShowTitleColor.setText(isChecked ? "默认Title文本颜色" : "自定义Title文本颜色");
        });

        sTmShowMsgColor.setCheckedStateChangedListener((absButton, isChecked) -> {
            isDefaultMsgColor = isChecked;
            sTvShowMsgColor.setText(isChecked? "默认Message文本颜色" : "自定义Message文本颜色");
        });

        sTmShowButtonColor.setCheckedStateChangedListener((absButton, isChecked) -> {
            isDefaultButtonColor = isChecked;
            sTvShowButtonColor.setText(isChecked ? "默认Botton文本颜色" : "自定义Botton文本颜色");
        });

        sTmShowBack.setCheckedStateChangedListener((absButton, isChecked) -> {
            isBackDim = isChecked;
            sTvShowBack.setText(isChecked ? "背景半透明" : "背景全透明");
        });

        sTmShowTitle.setChecked(true);
        sTmShowTitleColor.setChecked(true);
        sTmShowMsgColor.setChecked(true);
        sTmShowButtonColor.setChecked(true);
        sTmShowBack.setChecked(true);
        sBarNum.setProgressValue(1);

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_sBtn_titleAlert:
                sTmShowTitle.setChecked(!sTmShowTitle.isChecked());
                break;
            case ResourceTable.Id_sBtn_titleColorAlert:
                sTmShowTitleColor.setChecked(!sTmShowTitleColor.isChecked());
                break;
            case ResourceTable.Id_sBtn_msgColorAlert:
                sTmShowMsgColor.setChecked(!sTmShowMsgColor.isChecked());
                break;
            case ResourceTable.Id_sBtn_buttonColorAlert:
                sTmShowButtonColor.setChecked(!sTmShowButtonColor.isChecked());
                break;
            case ResourceTable.Id_sBtn_backAlert:
                sTmShowBack.setChecked(!sTmShowBack.isChecked());
                break;
            case ResourceTable.Id_rtv_showAlert:
                new UIAlertDialog.DividerIOSBuilder(this)
                        .setTitle(isShowTitle ? UIAlertDialog.class.getSimpleName() : "")
                        .setTitleTextColorResource(isDefaultTitleColor ? ResourceTable.Color_colorAlertTitle : ResourceTable.Color_holo_blue_dark)
                        .setMessage("1、本次更新修复多个重大BUG\n2、新增用户反馈接口")
                        .setMessageTextColorResource(isDefaultMsgColor ? ResourceTable.Color_colorAlertMessage : ResourceTable.Color_holo_green_dark)
                        .setNegativeButton(num > 1 ? "否定" : "", onAlertClick)
                        .setNegativeButtonTextColorResource(isDefaultButtonColor ? ResourceTable.Color_colorAlertButton : ResourceTable.Color_holo_red_dark)
                        .setPositiveButton("肯定", onAlertClick)
                        .setMinHeight(SizeUtil.dp2px(160))
                        .setPositiveButtonTextColorResource(isDefaultButtonColor ? ResourceTable.Color_colorAlertButton : ResourceTable.Color_holo_orange_dark)
                        .setNeutralButton(num > 2 ? "中性" : "", onAlertClick)
                        .setNeutralButtonTextColorResource(isDefaultButtonColor ? ResourceTable.Color_colorAlertButton : ResourceTable.Color_holo_purple)
                        .create()
                        .setDimAmount(isBackDim ? 0.6f : 0f)
                        .show();
                break;
            case ResourceTable.Id_rtv_showQqAlert:
                UIAlertDialog dialog = new UIAlertDialog.DividerQQBuilder(this)
                        .setTitle(isShowTitle ? "退出群聊" : "")
                        .setTitleTextColor(isDefaultTitleColor ? Color.BLACK : Color.BLUE)
                        .setMessage("你将退出  四川移动爱分享抢流量(XXXXXXXX) 退群通知仅群管理员可见。")
                        .setMessageTextColor(isDefaultMsgColor ? Color.BLACK : Color.GREEN)
                        .setNegativeButton(num > 1 ? "取消" : "", onAlertClick)
                        .setNegativeButtonTextColor(isDefaultButtonColor ? Color.BLACK : Color.RED)
                        .setPositiveButton("退出", onAlertClick)
                        .setPositiveButtonTextColor(isDefaultButtonColor ? Color.BLACK : Color.YELLOW)
                        .setNeutralButton(num > 2 ? "中性" : "", onAlertClick)
                        .setNeutralButtonTextColor(isDefaultButtonColor ? Color.BLACK : Color.BLUE)
                        .create()
                        .setDimAmount(isBackDim ? 0.6f : 0f);
                dialog.show();
                break;
            case ResourceTable.Id_rtv_showAllAlert:
                UIAlertDialog alertDialog = new UIAlertDialog.DividerQQBuilder(this)
                        //设置背景--包括根布局及Button
//                        .setBackgroundColor(Color.WHITE)
//                        .setBackground(drawable)
//                        .setBackgroundResource(resId)
                        //设置按下背景--Button
                        .setBackgroundPressedColor(Color.argb(255, 240, 240, 240))
//                        .setBackgroundPressed(drawable)
//                        .setBackgroundPressedResource(resId)
                        //背景圆角(当背景及按下背景为ColorDrawable有效)-根布局及Button
                        .setBackgroundRadius(6f)
//                        .setBackgroundRadiusResource(resId)
                        //设置统一padding
                        .setPadding(SizeUtil.dp2px(20))
                        //设置根布局最小高度
                        .setMinHeight(SizeUtil.dp2px(160))
                        .setElevation(12f)

                        //设置Title上边分割线颜色--推荐
//                        .setTitleDividerColor(Color.RED.getValue())
                        .setTitleDividerColor(Color.argb(255, 255, 0, 0))
//                        .setTitleDividerResource(resId)
//                        .setTitleDivider(drawable)
                        //设置Title分割线高度
                        .setTitleDividerHeight(SizeUtil.dp2px(4))
//                        .setTitleDividerHeightResource(resId)
                        //设置TextView对应的尺寸单位
                        .setTextSizeUnit(Text.TextSizeType.VP)
                        .setLineSpacingExtra(0f)
                        .setLineSpacingMultiplier(1.0f)
                        //设置Title文本
                        .setTitle("UIAlertDialog示例头部")
//                        .setTitle(resId)
                        //设置Title文本颜色
                        .setTitleTextColor(Color.BLACK)
//                        .setTitleTextColor(ColorStateList)
//                        .setTitleTextColorResource(resId)
                        //设置Title文本尺寸
                        .setTitleTextSize(20)
                        //设置Title文本对齐方式
                        .setTitleTextGravity(TextAlignment.CENTER)
                        //设置Title文本是否加粗
                        .setTitleTextFakeBoldEnable(false)

                        //设置Message文本
//                        .setMessage(Html.fromHtml(String.format(mFormatName, "你将退出 ", "四川移动爱分享抢流量(XXXXXXXX)", "退群通知仅群管理员可见。")))
                        .setMessage(String.format(mFormatName, "你将退出 ", "四川移动爱分享抢流量(XXXXXXXX)", "退群通知仅群管理员可见。"))
//                        .setMessage(resId)
                        //设置Message文本颜色
                        .setMessageTextColor(Color.BLACK)
//                        .setMessageTextColor(ColorStateList)
//                        .setMessageTextColorResource(resId)
                        //设置Message文本尺寸
                        .setMessageTextSize(16)
                        //设置Message文本对齐方式
                        .setMessageTextGravity(TextAlignment.CENTER)
                        //设置Title文本是否加粗
                        .setMessageTextFakeBoldEnable(false)

                        //设置View --始终在Message下边
//                        .setView(View)
//                        .setView(layoutId)

                        //设置是否去掉Button按下阴影-5.0以后的新特性
                        .setBorderLessButtonEnable(true)
                        //文本及点击事件
                        .setNegativeButton("取消", onAlertClick)
//                        .setNegativeButton(resId,click)
                        //文本颜色
                        .setNegativeButtonTextColor(Color.BLACK)
//                        .setNegativeButtonTextColor(ColorStateList)
//                        .setNegativeButtonTextColorResource(resId)
                        //文本尺寸
                        .setNegativeButtonTextSize(18)
                        //是否粗体
                        .setNegativeButtonFakeBoldEnable(false)

                        //文本及点击事件
                        .setNeutralButton("考虑", onAlertClick)
//                        .setNeutralButton(resId,click)
                        //文本颜色
                        .setNeutralButtonTextColor(Color.BLACK)
//                        .setNeutralButtonTextColor(ColorStateList)
//                        .setNeutralButtonTextColorResource(resId)
                        //文本尺寸
                        .setNeutralButtonTextSize(18)
                        //是否粗体
                        .setNeutralButtonFakeBoldEnable(false)

                        //文本及点击事件
                        .setPositiveButton("退出", onAlertClick)
//                        .setPositiveButton(resId,click)
                        //文本颜色
                        .setPositiveButtonTextColor(Color.BLACK)
//                        .setPositiveButtonTextColor(ColorStateList)
//                        .setPositiveButtonTextColorResource(resId)
                        //文本尺寸
                        .setPositiveButtonTextSize(18)
                        //是否粗体
                        .setPositiveButtonFakeBoldEnable(false)

                        //设置点击返回键是否可关闭Window
                        .setCancelable(true)
                        //设置点击非布局是否关闭Window
                        .setCanceledOnTouchOutside(true)
                        //设置Window dismiss()监听
                        .setOnDismissListener(new DialogInterface.OnDismissListener() {
                            @Override
                            public void onDismiss(CommonDialog dialog) {

                            }
                        })
                        //设置Window cancel()监听
                        .setOnCancelListener(new DialogInterface.OnCancelListener() {
                            @Override
                            public void onCancel(CommonDialog dialog) {

                            }
                        })
                        //设置 window show()监听
                        .setOnShowListener(new DialogInterface.OnShowListener() {
                            @Override
                            public void onShow(CommonDialog dialog) {

                            }
                        })
                        //设置Window 键盘事件监听
                        .setOnKeyListener(new DialogInterface.OnKeyListener() {
                            @Override
                            public boolean onKey(CommonDialog dialog, int keyCode, KeyEvent event) {
                                return false;
                            }
                        })
                        .setOnTextViewLineListener(new BasisDialog.OnTextViewLineListener() {
                            @Override
                            public void onTextViewLineListener(Text textView, int lineCount) {
                                switch (textView.getId()) {
                                    case ResourceTable.Integer_tv_titleAlertDialog:
                                        break;
                                    case ResourceTable.Integer_tv_messageAlertDialog:
                                        break;
                                    case ResourceTable.Integer_btn_negativeAlertDialog:
                                        break;
                                    case ResourceTable.Integer_btn_neutralAlertDialog:
                                        break;
                                    case ResourceTable.Integer_btn_positiveAlertDialog:
                                        break;
                                }
                            }
                        })

                        //创建Dialog
                        .create()
                        //设置Window宽度
                        .setWidth(ComponentContainer.LayoutConfig.MATCH_CONTENT)
                        //设置Window高度
                        .setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT)
                        //设置Window 阴影程度
                        .setDimAmount(0.6f)
                        //设置window其它属性
//                        .setAttributes(WindowManager.LayoutParams)
                        //设置window动画
//                        .setWindowAnimations(resId)
                        //设置Window 位置
                        .setGravity(LayoutAlignment.CENTER);
                alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
                alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL);
                alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
                alertDialog.show();
                break;
            case ResourceTable.Id_rtv_editAlert:
                final RadiusEditText editText = new RadiusEditText(AlertAbilitySlice.this);
                ComponentContainer.LayoutConfig params = new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
//                params.topMargin = SizeUtil.dp2px(12);
                editText.getDelegate()
                        .setTextColor(Color.GRAY.getValue())
                        .setRadius(6f)
                        .setLeftDrawableWidth(SizeUtil.dp2px(16))
                        .setLeftDrawableHeight(SizeUtil.dp2px(16))
                        .setLeftDrawable(ResourceTable.Media_ic_search_normal)
                        .setBackgroundColor(Color.WHITE.getValue())
                        .setStrokeColor(Color.GRAY.getValue())
                        .setStrokeWidth(SizeUtil.dp2px(0.5f));
                editText.setMinHeight(SizeUtil.dp2px(40));
                editText.setTextAlignment(TextAlignment.VERTICAL_CENTER);
                editText.setAroundElementsPadding(SizeUtil.dp2px(6));
                editText.setPadding(SizeUtil.dp2px(12), 0, SizeUtil.dp2px(12), 0);
                editText.setBubbleElement(new ResourceUtil(this).getColorDrawable(ResourceTable.Color_cursor_bubble_color));
                editText.setTextSize(14, Text.TextSizeType.FP);
                editText.setHint("请输入内容");
                editText.setLayoutConfig(params);
                new UIAlertDialog.DividerIOSBuilder(this)
                        .setTitle(isShowTitle ? UIAlertDialog.class.getSimpleName() : "")
                        .setTitleTextColorResource(isDefaultTitleColor ? ResourceTable.Color_colorAlertTitle : ResourceTable.Color_holo_blue_dark)
                        .setView(editText)
                        .setNegativeButton(ResourceTable.String_cancel, null)
                        .setPositiveButton(ResourceTable.String_ensure, new IDialog.ClickedListener() {
                            @Override
                            public void onClick(IDialog iDialog, int i) {
                                String text = editText.getText().toString().trim();
                                if (text == null || text.length() == 0) {
                                    return;
                                }
                                showToast("你输入的是:" + text);
                            }
                        })
                        .create()
                        .setDimAmount(isBackDim ? 0.6f : 0f)
                        .setAlignment(LayoutAlignment.BOTTOM)
                        .show();
                //决定是否自动弹出软键盘
                KeyboardHelper.openKeyboard(editText);
                break;
            case ResourceTable.Id_rtv_fragmentAlert:
                //系统暂无DialogFraction
                new UIAlertDialog.DividerQQBuilder(getContext())
                        .setTitle("退出群聊")
                        .setTitleTextColor(Color.BLACK)
                        .setMessage("你将退出  四川移动爱分享抢流量(XXXXXXXX) 退群通知仅群管理员可见。")
                        .setMessageTextColor(Color.BLACK)
                        .setNegativeButton("取消", null)
                        .setNegativeButtonTextColor(Color.BLACK)
                        .setPositiveButton("退出", null)
                        .setPositiveButtonTextColor(Color.BLACK)
                        .setNeutralButton("中性", null)
                        .setNeutralButtonTextColor(Color.BLACK)
                        .create()
                        .show();
                break;
            default:
                break;
        }
    }

    private void showToast(String msg) {
        ToastDialog toastDialog = new ToastDialog(this);
        toastDialog.setAlignment(LayoutAlignment.BOTTOM)
                .setAutoClosable(true)
                .setOffset(0, 100)
                .setDuration(1000)
                .setText(msg)
                .show();
    }

}
